package com.yztc.ui.classify.myadapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.yztc.bean.classify.ClassifyDataBean;
import com.yztc.chunboapp.R;
import com.yztc.ui.classify.ClassifyFragment;
import com.yztc.utils.ImageHelper;

import java.util.List;

/**
 * Created by MengLingzhang on 2016/10/7.
 */
public class GridAdapter extends XRecyclerView.Adapter<GridAdapter.MyViewHolder>{
    private Context context;
    private List<ClassifyDataBean> list;

    //将定义事件的接口作为适配器的属性
    private ClassifyFragment.onItemClickListener onItemClickListener;
    public void setOnItemClickListener(ClassifyFragment.onItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public GridAdapter(Context context,List<ClassifyDataBean> list){
        this.context=context;
        this.list=list;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder=new MyViewHolder(LayoutInflater.from
                (context).inflate(R.layout.item_classify_gridview,parent,false));
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        ImageHelper.getNormalImage(context,list.get(position).getIcon_url(),holder.iv);

        //判断当前控件是否设置了监听
        if(onItemClickListener!=null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //获取当前点击的item的下标
                    int pos=holder.getLayoutPosition()-1;
                    onItemClickListener.onItemClick(holder.itemView,pos);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos=holder.getLayoutPosition()-1;
                    onItemClickListener.onItemLongClick(holder.itemView,pos);
                    return false;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size()-1;
    }

    class MyViewHolder extends XRecyclerView.ViewHolder{
        ImageView iv;
        public MyViewHolder(View itemView) {
            super(itemView);
            iv= (ImageView) itemView.findViewById(R.id.iv_item_classify_gridview);
        }
    }
}
